package com.project.dao.impl;

import com.project.bean.CutPageBean;
import com.project.bean.LinkBean;
import com.project.dao.ILinkDao;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 通讯录持久接口实现类
 */
@Repository
public class LinkDaoImpl extends BaseDao implements ILinkDao {
    @Override
    public CutPageBean<LinkBean> cutAll(int pageNO) {
        CutPageBean<LinkBean> cutBean = new CutPageBean<LinkBean>();
        this.setConnection();

        try {
            ps = con.prepareStatement("select * from r_link limit ?,?");
            ps.setObject(1, (pageNO - 1) * CutPageBean.PAGESIZE);
            ps.setObject(2, CutPageBean.PAGESIZE);

            rs = ps.executeQuery();
            while (rs.next()) {
                LinkBean linkBean = this.getLink();
                cutBean.getList().add(linkBean);
            }

            ps = con.prepareStatement("select count(*) c from r_link");
            rs = ps.executeQuery();
            if (rs.next()) {
                cutBean.setCount(rs.getInt("c"));
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            this.closeConnection();
        }
        return cutBean;
    }

    /**
     * 删除用户
     *
     * @param id 用户id
     */
    @Override
    public void delUser(int id) {
        this.setConnection();
        try {
            ps = con.prepareStatement("delete from r_link where pk_linkId=?");
            ps.setObject(1, id);

            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.closeConnection();
    }

    /**
     * 添加用户
     *
     * @param linkBean 用户对象
     */
    @Override
    public void addUser(LinkBean linkBean) {
        this.setConnection();
        try {
            ps = con.prepareStatement("INSERT INTO r_link(l_name,l_birthday,l_phone)VALUES(?,?,?)");
            ps.setObject(1, linkBean.getName());
            ps.setObject(2, linkBean.getBirthday());
            ps.setObject(3, linkBean.getPhone());

            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.closeConnection();
    }

    /**
     * 修改用户电话
     *
     * @param id    用户id
     * @param phone 修改后的电话
     */
    @Override
    public void update(int id, String phone) {
        this.setConnection();
        try {
            ps = con.prepareStatement("update r_link set l_phone = ? where pk_linkId = ?");
            ps.setObject(1, phone);
            ps.setObject(2, id);

            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.closeConnection();
    }

    @Override
    public LinkBean findById(int id) {
        LinkBean linkBean = new LinkBean();
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from r_link where pk_linkId = ?");
            ps.setObject(1,id);
            ps.setObject(1,id);
            rs = ps.executeQuery();
            while (rs.next()){
                linkBean.setBirthday(LocalDate.parse(rs.getString("l_birthday")));
                linkBean.setName(rs.getString("l_name"));
                linkBean.setId(rs.getInt("pk_linkId"));
                linkBean.setPhone(rs.getString("l_phone"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return linkBean;
    }


    private LinkBean getLink() throws SQLException {
        LinkBean link = new LinkBean();
        link.setId(rs.getInt("pk_linkId"));
        link.setName(rs.getString("l_name"));
        link.setBirthday(LocalDate.parse(rs.getString("l_birthday")));
        link.setPhone(rs.getString("l_phone"));
        return link;
    }

}
